//
//  XHLMacroDefin.h
//  XHLKit
//
//  Created by qinyulun on 16/6/18.
//  Copyright © 2016年 leTian. All rights reserved.
//

#ifndef XHLMacroDefin_h
#define XHLMacroDefin_h

//测试环境输出日志
#ifdef DEBUG
#define LTLog(FORMAT, ...) fprintf(stderr,"%s:%d\t%s\n",[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#else
#define LTLog(FORMAT, ...) nil

#endif
//end

//设备类
#define IOS [[[UIDevice currentDevice] systemVersion] floatValue]

#define IOS7 ( IOS >= 7.0)
#define IOS8 ( IOS >= 8.0)
#define IOS9 ( IOS >= 9.0)
#define IOS4 (IOS < 5.0)

//屏幕尺寸适配
#define SCREEN_WIDTH  [[UIScreen mainScreen] bounds].size.width
#define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height
#define SCREEN_SIZE   [[UIScreen mainScreen] bounds].size

#define NavStateBarHight (20)
#define NavBarHight (44)
#define NavBarAndStateBarHight (NavStateBarHight + NavBarHight)
//END

//视图尺寸
#define WIDTH(view) view.frame.size.width
#define HEIGHT(view) view.frame.size.height
#define IMGWIDTH(image) image.size.width
#define IMGHEIGHT(image) image.size.height
#define X(view) view.frame.origin.x
#define Y(view) view.frame.origin.y

#define X_CENTER(view) view.center.x
#define Y_CENTER(view) view.center.y

#define MaxX(view) CGRectGetMaxX(view.frame)
#define MaxY(view) CGRectGetMaxY(view.frame)

#define MaxScreenX CGRectGetWidth([UIScreen mainScreen].bounds)
#define MaxScreenY CGRectGetHeight([UIScreen mainScreen].bounds)

#define LEFT(view) view.frame.origin.x
#define TOP(view) view.frame.origin.y
#define BOTTOM(view) (view.frame.origin.y + view.frame.size.height)
#define RIGHT(view) (view.frame.origin.x + view.frame.size.width)

#define MinX(v)                 CGRectGetMinX((v).frame)
#define MinY(v)                 CGRectGetMinY((v).frame)

#define MidX(v)                 CGRectGetMidX((v).frame)
#define MidY(v)                 CGRectGetMidY((v).frame)
//end

//视图相对高
#define CENTER_VERTICALLY(parent,child) floor((parent.frame.size.height - child.frame.size.height) / 2)
#define CENTER_HORIZONTALLY(parent,child) floor((parent.frame.size.width - child.frame.size.width) / 2)

// example: [[UIView alloc] initWithFrame:(CGRect){CENTER_IN_PARENT(parentView,500,500),CGSizeMake(500,500)}];
#define CENTER_IN_PARENT(parent,childWidth,childHeight) CGPointMake(floor((parent.frame.size.width - childWidth) / 2),floor((parent.frame.size.height - childHeight) / 2))
#define CENTER_IN_PARENT_X(parent,childWidth) floor((parent.frame.size.width - childWidth) / 2)
#define CENTER_IN_PARENT_Y(parent,childHeight) floor((parent.frame.size.height - childHeight) / 2)
//end

//颜色和透明度设置
#define RGBA(r,g,b,a)  [UIColor colorWithRed:(float)r/255.0f green:(float)g/255.0f blue:(float)b/255.0f alpha:a]
//设置颜色值 #acacac
#define COLOR(colorString) [GET_SINGLETON_FOR_CLASS(HCColor) colorWithHexString:colorString]
//END

// 定义UIImage对象
#define ImageNamed(_pointer) [UIImage imageNamed:_pointer]

//字符串尺寸
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
#define kGregorianCalendar  NSCalendarIdentifierGregorian
#define kSizeWithFont(NSString,UIFont) [NSString sizeWithAttributes:@{NSFontAttributeName:UIFont}]
#else
#define kGregorianCalendar  NSGregorianCalendar
#define kSizeWithFont(NSString,UIFont) [NSString sizeWithFont:UIFont constrainedToSize:CGSizeMake(INT32_MAX, INT32_MAX) lineBreakMode:NSLineBreakByTruncatingTail]
#endif

#pragma mark - 数组 or 字典 读取宏

#define DicObjectForKey(obj,dictionary,key); @try\
{\
obj = [dictionary objectForKey:key];\
}\
@catch(NSException * exception)\
{\
NSLog(@"dictionary exception:%@",exception);\
}\
@finally\
{\
}

#define DicAddObject(dic,value,key);if(value != nil && key != nil){\
[dic setObject:value forKey:key]; \
}


#define ArrayReplaceObjectAtIndex(obj,array,index); @try\
{\
[array replaceObjectAtIndex:index withObject:obj];\
}\
@catch(NSException * exception)\
{\
NSLog(@"Array replace cross-border exception:%@",exception);\
}\
@finally\
{\
}

#define ArrayRemoveObjectAtIndex(array,index); @try\
{\
[array removeObjectAtIndex:index];\
}\
@catch(NSException * exception)\
{\
NSLog(@"Array remove cross-border exception:%@",exception);\
}\
@finally\
{\
}

#define ArrayObjectAtIndex(obj,array,index); @try\
{\
obj = [array objectAtIndex:index];\
}\
@catch(NSException * exception)\
{\
NSLog(@"Array cross-border exception:%@",exception);\
}\
@finally\
{\
}


#define NotifiDefaultCenter [NSNotificationCenter defaultCenter]
// 通知移除
#define LTNotifiRemove(id,name,objs)  [NotifiDefaultCenter removeObserver:id name:obj object:objs];
//发送通知
#define LTNotifiPost(name,obj) [[NSNotificationCenter defaultCenter] postNotificationName:name object:obj]
#define LTNotifiPostDic(name,obj,dic) [[NSNotificationCenter defaultCenter] postNotificationName:name object:obj userInfo:dic]

//正常字体
#define kFontHeitiRegular_26 [UIFont fontWithName:@"Heiti SC" size:26]
#define kFontHeitiRegular_32 [UIFont fontWithName:@"Heiti SC" size:32]
#define kFontHeitiRegular_24 [UIFont fontWithName:@"Heiti SC" size:24]
#define kFontHeitiRegular_22 [UIFont fontWithName:@"Heiti SC" size:22]
#define kFontHeitiRegular_20 [UIFont fontWithName:@"Heiti SC" size:20]
#define kFontHeitiRegular_18 [UIFont fontWithName:@"Heiti SC" size:18]
#define kFontHeitiRegular_16 [UIFont fontWithName:@"Heiti SC" size:16]
#define kFontHeitiRegular_15 [UIFont fontWithName:@"Heiti SC" size:15]
#define kFontHeitiRegular_14 [UIFont fontWithName:@"Heiti SC" size:14]
#define kFontHeitiRegular_13 [UIFont fontWithName:@"Heiti SC" size:13]
#define kFontHeitiRegular_12 [UIFont fontWithName:@"Heiti SC" size:12]
#define kFontHeitiRegular_10 [UIFont fontWithName:@"Heiti SC" size:10]
#define kFontHeitiRegular_9  [UIFont fontWithName:@"Heiti SC" size:9]
#define kFontHeitiRegular_8  [UIFont fontWithName:@"Heiti SC" size:8]
#define kFontHeitiRegular_7  [UIFont fontWithName:@"Heiti SC" size:7]
#define kFontHeitiRegular_11 [UIFont fontWithName:@"Heiti SC" size:11]
#define kFontHeitiRegularBold_16 [UIFont fontWithName:@"Heiti SC-Bold" size:16]
#define kFontHeitiRegularBold_6 [UIFont fontWithName:@"Heiti SC-Bold" size:6]

//常用的颜色
#define KColor_0             COLOR(@"#ececec") //[uicolor clear]
#define KColor_1             COLOR(@"#ff3636") //红色
#define KColor_2             COLOR(@"#7a7a7a")
#define KColor_3             COLOR(@"#ffffff") //白色
#define KColor_4             COLOR(@"#bebebe") //灰色
#define KColor_5             COLOR(@"#4e4e4e") //黑色
#define KColor_6             COLOR(@"#e3e3e3") //灰色
#define KColor_7             COLOR(@"#efefef") //亮灰色
#define KColor_8             COLOR(@"#f7f7f7") //灰色
#define KColor_9             COLOR(@"#f3f3f3") //灰色
#define KColor_10            COLOR(@"#BFBFBF")//深灰色
#define KColor_11            COLOR(@"#D4D4D4") //灰色(分割线)
#define KColor_12            COLOR(@"#FFFF00") //亮黄色
#define KColor_13            COLOR(@"#FFC125") //暗黄色
#define KColor_14            COLOR(@"#FF0000") //红色
#define KColor_15            COLOR(@"#FF8C00") //橘色
#define KColor_16            COLOR(@"#C0FF3E") //亮绿色
#define KColor_17            COLOR(@"#1C86EE") //天蓝色
#define KColor_18            COLOR(@"#00FFFF") //大海蓝色
#define KColor_19            COLOR(@"#CD8500") //棕色
#define NavBarBG             COLOR(@"#00B44A") //导航栏颜色
#define KColor_20            COLOR(@"#F5F5F5") //白烟灰  淡色分割线
#define KColor_21            COLOR(@"#B0171F") //深红
#define KColor_22            COLOR(@"#24B0C0") //蓝色
#define KColor23            COLOR(@"#70292421")//遮罩层
#define KColor24            COLOR(@"#8C735D")//棕色
#define Kcolor25            COLOR(@"#424B4F")//深墨绿色
#define KColor26            COLOR(@"#E6513F") //红色 项目经理
#define KColor27            COLOR(@"#669238") //绿色 项目经理
#define KColor28            COLOR(@"#499DEF")  //蔚蓝色
#define KColor29            COLOR(@"#FS6B36") //橘黄色
#define KColor_30           COLOR(@"#88FDFEFF") //白色遮罩层
#define KColor_31           COLOR(@"#9BABB4")   //浅灰色
#define KColor_32           COLOR(@"#dedede")   //提醒文字浅色
#define KColor_33           COLOR(@"#F19C4A")  //橘黄色（常用）
#define KColor_a            COLOR(@"#444444")
#define KColor_b            COLOR(@"#777777")
#define KColor_c            COLOR(@"#7b000000")
#define KColor_d            COLOR(@"#7bffffff")
#define KColor_e            COLOR(@"#00c0c0")
#define KColor_f            COLOR(@"#a0a0a0")
#define KColor_g            COLOR(@"#c0c0c0")
#define KColor_h            COLOR(@"#e5e5e5")    //编辑框边框颜色

#define kColor_Black        [UIColor blackColor]
#define kColor_White        [UIColor whiteColor]
#define kColor_Gray         [UIColor grayColor]
#define kColor_LightGray    [UIColor lightGrayColor]
#define kColor_Red          [UIColor redColor]
//END

// View 圆角和加边框
#define ViewBorderRadius(View, Radius, Width, Color)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]

// View 圆角
#define ViewRadius(View, Radius)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES]

// 时间间隔
#define kHUDDuration            (1.f)
// 一天的秒数
#define SecondsOfDay            (24.f * 60.f * 60.f)
// 秒数
#define Seconds(Days)           (24.f * 60.f * 60.f * (Days))
// 一天的毫秒数
#define MillisecondsOfDay       (24.f * 60.f * 60.f * 1000.f)
// 毫秒数
#define Milliseconds(Days)      (24.f * 60.f * 60.f * 1000.f * (Days))

//动画时间
#define kDurationSmall          0.25f
#define KDurationNormal         0.5f
#define kDurationNormal_Max     0.75f
#define kDurationBig            1.0f

// block self
#pragma mark - block引用
#define LTWeakSelf(type) __weak typeof(type) weak##type = type;
#define LTStrongSelf()   __strong typeof(type) type = weak##type;

//清除背景色
#define CLEARCOLOR [UIColor clearColor]

//G－C－D
#define BACK(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
#define MAIN(block) dispatch_async(dispatch_get_main_queue(),block)

//NSUserDefaults 实例化
#define USER_DEFAULT [NSUserDefaults standardUserDefaults]

//由角度获取弧度 有弧度获取角度
#define degreesToRadian(x) (M_PI * (x) / 180.0)
#define radianToDegrees(radian) (radian*180.0)/(M_PI)

//判断对象是否为空
static inline BOOL isEmpty(id thing) {
    return thing == nil || [thing isEqual:[NSNull null]]
    || ([thing respondsToSelector:@selector(length)]
        && [(NSData *)thing length] == 0)
    || ([thing respondsToSelector:@selector(count)]
        && [(NSArray *)thing count] == 0);
}

//将对象转换为字符串
static inline NSString *StringFromObject(id object) {
    if (object == nil || [object isEqual:[NSNull null]]) {
        return @"";
    } else if ([object isKindOfClass:[NSString class]]) {
        return object;
    } else if ([object respondsToSelector:@selector(stringValue)]){
        return [object stringValue];
    } else {
        return [object description];
    }
}

static inline float widthForString(NSString* str,UIFont* font,float height)
{
    CGRect rect = [str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
    return rect.size.width;
}

static inline CGRect rectForString(NSString* str,UIFont* font,float height)
{
    CGRect rect = [str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
    return rect;
}

//去除空格
static inline NSString * stringWithSpace(NSString* string){
    NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
    
    NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];
    
    NSArray *parts = [string componentsSeparatedByCharactersInSet:whitespaces];
    
    NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
    
    return [filteredArray componentsJoinedByString:@""];
}

static inline NSString* stringWithNull(NSString* string){
    NSString* returnString = string ;
    if (isEmpty(string)) {
        returnString = @"" ;
        return returnString;
    }
    if ([string rangeOfString:@"null"].location != NSNotFound) {
        returnString = @"" ;
    }
    if ([string rangeOfString:@"(null)"].location != NSNotFound) {
        returnString = @"" ;
    }
    return returnString ;
}

/**
 *  邮箱验证
 *
 *  @param email 邮箱
 *
 *  @return
 */
static inline  BOOL validateEmail(NSString *email)
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}


/**
 *  手机号码判断 正则表达式
 *
 *  @param mobile 手机号
 *
 *  @return
 */
static inline  BOOL validateMobile(NSString *mobile)
{
    NSString *phoneRegex = @"^(1[3-9])\\d{9}$";
    
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    
    
    return [phoneTest evaluateWithObject:mobile];
}


#endif /* XHLMacroDefin_h */
